// 引入express
const express = require("express");
// 引入数据库
const sql = require("../pool.js");
// 创建路由器对象
const route = express.Router();
// 订单增加路由
route.post("/orderAdd", (req, res) => {
  sql.query("insert into zm_product set ?", [req.body], (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);
  });
  if (r.affectedRows) {
    res.send("增加成功!");
  } else {
    res.send("增加失败!");
  }
});
// 查询订单信息路由
route.get("/orderInfo", (req, res) => {
  sql.query("select * from zm_product", (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);
    if (r.length) {
      res.send({ code: 200, msg: "查询成功", data: r });
    } else {
      res.send({ code: 400, msg: "查询失败" });
    }
  });
});
// 订单更改路由
route.put("/orderUpdate", (req, res) => {
  sql.query(
    "update zm_product set ? where pid=?",
    [req.body, req.body.pid],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);
    }
  );
  if (r.affectedRows) {
    res.send("更改成功!");
  } else {
    res.send("更改失败!");
  }

  console.log(req.body);
});
// 订单删除路由
route.get("/orderDelete/:danHao", (req, res) => {
  sql.query(
    "delete from zm_user where danHao = ?",
    [req.params.danHao],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);
    }
  );
  if (r.affectedRows) {
    res.send("删除成功!");
  } else {
    res.send("删除成功!");
  }
});
module.exports = route;
